using System;
using System.Collections.Generic;
using System.Text;
using Ghasam.Business.Domain.Entity;
using NHibernate;
using NHibernate.Expression;
using System.Collections;

namespace Ghasam.Business.DAO.Impl
{
    public class StockHistoryDao : DaoBase<StockHistory>, IStockHistoryDao
    {
        public IList<StockHistory> FindStockHistory(string name, string binnumber, string serial, int? status, int? employeeid, DateTime? exitDate, DateTime? returnDate, int pageSize, int pageNo)
        {
            ICriteria cri = base.Session.CreateCriteria(typeof(StockHistory));
            cri.CreateAlias("Stock", "stock");
            cri.CreateAlias("StockStatus", "stockStatus");
            if (name.Length > 0)
            {
                cri.Add(Expression.Like("stock.StockName",name,MatchMode.Anywhere));
            }

            if (binnumber.Length > 0)
            {
                cri.Add(Expression.Like("stock.BinNumber", binnumber, MatchMode.Anywhere));
            }

            if (serial.Length > 0)
            {
                cri.Add(Expression.Eq("stock.Serial",serial));
            }

            if (status.HasValue && status > 0)
            {
                cri.Add(Expression.Eq("stockStatus.Id", status.Value));
            }

            if (exitDate.HasValue)
            {
                cri.Add(Expression.Eq("ExitDate", exitDate.Value ));
            }
            if (returnDate.HasValue)
            {
                cri.Add(Expression.Eq("ReturnDate",returnDate.Value)); ;
            }

            //cri.SetMaxResults(pageSize * pageNo);
            cri.SetFirstResult(pageSize * pageNo);
            cri.AddOrder(Order.Desc("stock.Id"));
            
            return cri.List<StockHistory>();
        }
    }
}
